package cn.study.controller;

import cn.study.entity.StudyEmployee;
import cn.study.entity.StudyRoleMenu;
import cn.study.service.StudyEmployeeService;
import cn.study.service.StudyMenuService;
import cn.study.service.StudyRoleMenuService;
import cn.study.service.StudyRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @date 2020/6/23 -15:26
 */
@Controller

public class LoginController {
    @Autowired
    StudyEmployeeService studyEmployeeService;
    @Autowired
    StudyRoleService studyRoleService;
    @Autowired
    StudyMenuService studyMenuService;

    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        Model model, HttpSession session
    ) {
        HashMap<String, Object> map = new HashMap<>();
        // 自定义要查询
        map.put("user_name", username);
        map.put("password", password);
        StudyEmployee studyEmployee = studyEmployeeService.login(map);
        if (studyEmployee == null) {
            model.addAttribute("msg", "账号或密码错误");
            return "login";
        } else if (studyEmployee.getEmpState() != 1) {
            String str = studyEmployee.getEmpState() == 2 ? "你已离职" : "你已被辞退🤬";
            model.addAttribute("msg", str);
            return "login";
        }/* else if (!"管理员".equals(studyRoleService.getById(studyEmployee.getEmpRoleId()).getRoleName())) {
            model.addAttribute("msg", "你不是管理员你没有权限哟👉");
            return "login";
        }*/
        session.setAttribute("ONEMENU", studyMenuService.studyRolelist(studyEmployee.getEmpRoleId(), 1));
        session.setAttribute("TWOMENU", studyMenuService.studyRolelist(studyEmployee.getEmpRoleId(), 2));
        session.setAttribute("THREEMENU", studyMenuService.studyRolelist(studyEmployee.getEmpRoleId(), 3));
        session.setAttribute("USER", studyEmployee);
        return "index";
    }
}